package com.ciban.smarthome.module.ui.adapter;

import java.util.ArrayList;

import com.ciban.smarthome.R;
import com.ciban.smarthome.common.utils.ImgUtils;
import com.ciban.smarthome.common.utils.logcrash.LogUtil;
import com.ciban.smarthome.module.entity.Equip_Lamp;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class LightsLvAdapter extends BaseAdapter {

	public interface LightAdapterListener {
		void LightStatusChanged(int position, boolean checked);

		void LightLevelChanged(int position, int progress);

		void clickItem(int position);

		boolean longClickItem(int position);
	}

	private Context context;
	private ArrayList<Equip_Lamp> list;
	private LightAdapterListener listener;

	public LightsLvAdapter(Context context, ArrayList<Equip_Lamp> list) {
		this.context = context;
		this.list = list;
	}

	public void setListener(LightAdapterListener listener) {
		this.listener = listener;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = View.inflate(context,
					R.layout.listitem_lamp_info, null);
			holder = new ViewHolder();
			holder.bar = (SeekBar) convertView
					.findViewById(R.id.light_item_seek);
			holder.iv_icon = (ImageView) convertView
					.findViewById(R.id.light_item_icon);
			holder.tv_name = (TextView) convertView
					.findViewById(R.id.light_item_name);
			holder.cb_status = (CheckBox) convertView
					.findViewById(R.id.light_item_status);
			holder.ll_head = (LinearLayout) convertView
					.findViewById(R.id.light_item_head);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final Equip_Lamp info = list.get(position);

		holder.tv_name.setText(info.getName());

		holder.iv_icon.setImageBitmap(ImgUtils.getImageFromAssetsFile(context,
				info.getIcon_path()));

		if (info.getLevel() < 0) {
			holder.bar.setEnabled(false);
		} else {
			holder.bar.setEnabled(true);
		}

		if (info.getStatus() == 1) {
			holder.cb_status.setChecked(true);
		} else if (info.getStatus() == 0) {
			holder.cb_status.setChecked(false);
		}

		holder.cb_status
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						if (listener != null) {
							listener.LightStatusChanged(position, isChecked);
						}
					}
				});

		holder.bar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {

			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {

			}

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				LogUtil.info("灯的拖动进度：" + progress);
				if (listener != null) {
					listener.LightLevelChanged(position, progress);
				}
			}
		});

		holder.ll_head.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (listener != null)
					listener.clickItem(position);
			}
		});

		holder.ll_head.setOnLongClickListener(new OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {
				if (listener != null)
					return listener.longClickItem(position);
				return false;
			}
		});

		return convertView;
	}

	static class ViewHolder {
		SeekBar bar;
		CheckBox cb_status;
		TextView tv_name;
		ImageView iv_icon;
		LinearLayout ll_head;
	}

}
